package com.hefeng.wx.handler;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.common.redis.service.RedisService;
import com.hefeng.wx.common.StartLoadingParameter;
import com.hefeng.wx.domain.WxDeviceReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class DeviceReportRedisHandler {
    @Autowired
    private RedisService redisService;
    /**
     * 设置设备信息进缓存内
     * */
    public WxDeviceReport setDeviceReportRedis(WxDeviceReport wxDeviceReport){
        WxDeviceReport wd=redisService.getCacheMapValue(StartLoadingParameter.DEVICEREPORT,wxDeviceReport.getDeviceCode());
        if(EmptyUtils.isEmpty(wd)){//如果为空第一次报警
            redisService.setCacheMapValue(StartLoadingParameter.DEVICEREPORT,wxDeviceReport.getDeviceCode(),wxDeviceReport);
            return wxDeviceReport;
        }


        return wd;
    }
    /**
     * 判断当前设备是否3分钟内重复报警
     * */
    public boolean isRepeatReport(WxDeviceReport wxDeviceReport){
        WxDeviceReport wd=redisService.getCacheMapValue(StartLoadingParameter.DEVICEREPORT,wxDeviceReport.getDeviceCode());
        long min = DateUtil.between(wxDeviceReport.getReportTime(), wd.getReportTime(), DateUnit.MINUTE);
        if (min > 3) {
            return true;
        }
        return  false;
    }


}
